package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.BuyPlayer;
import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.Player;
import edu.its.dreamteam.daf.beans.Room;
import edu.its.dreamteam.daf.beans.SellPlayer;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IBuyPlayerDao;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.IPlayerDao;
import edu.its.dreamteam.daf.dao.IRoomDao;
import edu.its.dreamteam.daf.dao.ISellPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class RoomGrid extends SecureBasePage {
	@Inject
	private IRoomDao roomDao;
	@Inject
	private IDraftPlayerDao draftPlayerDao;
	@Inject
	private ITeamDao TeamDao;
	@Inject
	private IBuyPlayerDao buyPlayerDao;
	@Inject
	private ISellPlayerDao sellPlayerDao;
	
	private List<Room> roomList;
	private Room room;
	private Team team;
	
	@Retain
	private BeanModel roomModel;
	@Inject
	private BeanModelSource roomModelSource;
    @Inject
    private ComponentResources resources;
      
    private boolean delete;
    private List<String> deletedList;
        
    
	public boolean isDelete() {
		return delete;
	}
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(getRoom().getIdRoom());
		}
		this.delete = delete;
	}
	public List<String> getDeletedList() {
		if(deletedList==null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}
	
	public Room getRoom() {
		return room;
	}
	public void setRoom(Room room) {
		this.room = room;
	}
	
	public Team getTeam() {
		if(team==null){
			team = (Team)getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return team;
	}
	public void setTeam(Team team) {
		this.team = team;
	}
	public IRoomDao getRoomDao() {
		return roomDao;
	}
	
	public ISellPlayerDao getSellPlayerDao() {
		return sellPlayerDao;
	}
	public IBuyPlayerDao getBuyPlayerDao() {
		return buyPlayerDao;
	}
	public ITeamDao getTeamDao() {
		return TeamDao;
	}
	
	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}
	public List<Room> getRoomList() {
		if(roomList==null){
			roomList = new ArrayList<Room>();
			roomList = getRoomDao().queryForList("", "");
			
		}
		return roomList;
	}
	public void setRoomList(List<Room> roomList) {
		this.roomList = roomList;
	}
	
	public BeanModel getRoomModel() {
		return roomModel;
	}
	
	void pageLoaded(){
    	roomModel = roomModelSource.create(Room.class, false, resources);
    	roomModel.add("actions", null).label("Edit");	
    	roomModel.reorder("actions");
    }	
	
	Object onSuccessFromPositionForm(){
		
		return PositionGrid.class;
	}
	
	
	void onActionFromRoomValidation(){

		List<DraftPlayer> draftPlayerList = getDraftPlayerDao().queryForList(" \"status_draft\" ='0' ", "");
		if(draftPlayerList.size()!=0){
			for(DraftPlayer draftPlayerObj : draftPlayerList){
				draftPlayerObj.setStatusDraft("1");
				getDraftPlayerDao().update(draftPlayerObj);
			}
		}
		
		List<BuyPlayer> buyPlayerList = getBuyPlayerDao().queryForList("", "");
		if(buyPlayerList.size()!=0){
			for(BuyPlayer buyPlayerObj : buyPlayerList){
				DraftPlayer draftPlayerObj = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(buyPlayerObj.getSellPlayer().getIdDraftPlayer());
				draftPlayerObj.setIdTeam(buyPlayerObj.getIdTeam());
				draftPlayerObj.setSell("0");
				draftPlayerObj.setPlay("0");
				getDraftPlayerDao().update(draftPlayerObj);
				
				SellPlayer sellPlayerObj = (SellPlayer)getSellPlayerDao().findByPrimaryKey(buyPlayerObj.getIdSell());
				if(sellPlayerObj!=null){
					getSellPlayerDao().delete(sellPlayerObj);
					getBuyPlayerDao().delete(buyPlayerObj);
				}
				
			}
		}
		
	}
	
	
}
